package rin.android.gamble;
import java.util.ArrayList;
import java.util.Random;
import rin.android.database.ClassPlayer;
import rin.android.database.Database;
import rin.android.dialog.MessageDialog;
import rin.android.sound.MusicBackground;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class BaiCao extends MyApp {
	public static BaiCao mBaiCao;
	ImageView labai1,labai2,labai3,labai4,labai5,labai6,btn;
	TextView tv1,tv2,tv3,tvDollar;
	MessageDialog dialog;
	int sonut,sonut_player,sonut_computer=0;//de xet xem duoc may nut
	int diem = 0;
	boolean batay1,batay2=true;//de xet truong hop 3 tay
	String chuoikq1,chuoikq2;
	Context context;
	Database mData;
	ClassPlayer mPlayer;
	String userName;
	MusicBackground mMusic;
	ArrayList<String> cacladarut=new ArrayList<String>();
	ImageView mangimg[]=new ImageView[6];
	int manghinhbai[][]={
			{R.drawable.b2fv,
			R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4,R.drawable.c5,
			R.drawable.c6,R.drawable.c7,R.drawable.c8,R.drawable.c9,R.drawable.c10,
			R.drawable.cj,R.drawable.cq,R.drawable.ck},
			
			{R.drawable.b2fv,
			R.drawable.d1,R.drawable.d2,R.drawable.d3,R.drawable.d4,R.drawable.d5,
			R.drawable.d6,R.drawable.d7,R.drawable.d8,R.drawable.d9,R.drawable.d10,
			R.drawable.dj,R.drawable.dq,R.drawable.dk},
			
			{R.drawable.b2fv,
			R.drawable.h1,R.drawable.h2,R.drawable.h3,R.drawable.h4,R.drawable.h5,
			R.drawable.h6,R.drawable.h7,R.drawable.h8,R.drawable.h9,R.drawable.h10,
			R.drawable.hj,R.drawable.hq,R.drawable.hk},
			
			{R.drawable.b2fv,
			R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5,
			R.drawable.s6,R.drawable.s7,R.drawable.s8,R.drawable.s9,R.drawable.s10,
			R.drawable.sj,R.drawable.sq,R.drawable.sk}
	};
	String mangtenbai[][]={
			{"rong",
			"ach chuon","hai chuon","ba chuon","bon chuon","nam chuon",
			"sau chuon","bay chuon","tam chuon","chin chuon","muoi chuon",
			"boi chuon","dam chuon","gia chuon"},
			
			{"rong",
			"ach ro","hai ro","ba ro","bon ro","nam ro",
			"sau ro","bay ro","tam ro","chin ro","muoi ro",
			"boi ro","dam ro","gia ro"},
		
			{"rong",
			"ach co","hai co","ba co","bon co","nam co",
			"sau co","bay co","tam co","chin co","muoi co",
			"boi co","dam co","gia co"},
			
			{"rong",
			"ach bich","hai bich","ba bich","bon bich","nam bich",
			"sau bich","bay bich","tam bich","chin bich","muoi bich",
			"boi bich","dam bich","gia bich"}		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.baicao);
        Bundle extras = getIntent().getExtras();
        mBaiCao = this;
        context = this;
        //Nếu không có dữ liệu thì không làm gì nữa
        if (extras == null) {
            return;
        }
        //Nếu có dữ liệu thì lấy đưa vào chuỗi với key tương ứng
        userName = extras.getString("userName");
        String playerDollar = extras.getString("dollar");
        diem = Integer.parseInt(playerDollar);
        //
        //mPlayer = mData.getPlayerByName(userName);
        mMusic = new MusicBackground();
		mMusic.loadMusic(this);
        if (Setting.isMusic)
            mMusic.play();
        else{
            mMusic.play();
            mMusic.pause();
        }
		//LinearLayout layout = (LinearLayout) findViewById(R.id.root);
		//layout.setBackgroundColor(Color.parseColor("#61B8F2"));
		dialog = new MessageDialog();
		labai1=(ImageView) findViewById(R.id.playerCard1);
		labai2=(ImageView) findViewById(R.id.playerCard2);
		labai3=(ImageView) findViewById(R.id.playerCard3);
		
		labai4=(ImageView) findViewById(R.id.comCard1);
		labai5=(ImageView) findViewById(R.id.comCard2);
		labai6=(ImageView) findViewById(R.id.comCard3);
		
		mangimg[0]=labai1;mangimg[1]=labai2;mangimg[2]=labai3;mangimg[3]=labai4;mangimg[4]=labai5;mangimg[5]=labai6;
		
		btn=(ImageView) findViewById(R.id.button1);
		
		tv1 = (TextView) findViewById(R.id.player_baicao);	
		tv2 = (TextView) findViewById(R.id.com_baicao);
		tv3 = (TextView) findViewById(R.id.playinfo);
		tvDollar = (TextView) findViewById(R.id.dollar_baicao);
		tv3.setText("Player: "+userName);
		tvDollar.setText("$$$ "+playerDollar);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			if(diem>=100){
				ChiaBai();
			}
			else{
				MainActivity.mSound.playBad();
				dialog.showKhongDuTien(context);
			}
			}
		});
		
	}
	
	public void ChiaBai()
	{
		cacladarut.clear();
		//tao ngau nhien
		Random rd=new Random();
		int x,y;
		
		sonut=0;
		batay1=batay2=true;
		//tao ngau nhien bo vao ArrayList de xet xem co bi trung khong
		for(int i=0;i<mangimg.length;i++)
		{
			while(true)
			{
				x=rd.nextInt(4);//0->3 (b-a+1)+a;
				y=rd.nextInt(13)+1;//1->13
				if(cacladarut.contains(mangtenbai[x][y])==false)
				{
					cacladarut.add(mangtenbai[x][y]);
					break;
				}
				
			}
			
				mangimg[i].setImageResource(manghinhbai[x][y]);	
				
				//chi can y<11 lap tuc khong con la batay nua
				if(y<11)
				{
					batay1=false;
					batay2=false;
				}
									
				sonut+=(y<10)?y:0;
				
				if(i==2)
				{
					sonut_player=sonut%10;					
					if(batay1==true)
					{
						chuoikq1="Bạn : Ba tây";
						sonut_player=10;
					}
					else
						chuoikq1="Bạn : "+sonut_player+" nút";
					
					batay2=true;
					sonut=0;					
				}
				if(i==5)
				{
					sonut_computer=sonut%10;
					if(batay2==true)
					{
						chuoikq2="Computer : Ba tây";
						sonut_computer=10;
					}						
					else
						chuoikq2="Computer : "+sonut_computer+" nút";
				}
				
		}
		
		if(sonut_player>sonut_computer)
		{
			chuoikq1+="- Thắng";
			chuoikq2+="- Thua";
			dialog.showWinDialog(this);
			MainActivity.mSound.playGood();
			diem += 50;
			tvDollar.setText("$$$ " + diem + "");
		}
		else if(sonut_player<sonut_computer)
		{
			chuoikq2+="- Thắng";
			chuoikq1+="- Thua";
			MainActivity.mSound.playBad();
			dialog.showLoseDialog(this);
			diem -= 100;
			tvDollar.setText("$$$ " + diem + "");
		}
		else if(sonut_player==sonut_computer)
		{
			chuoikq1+="- Hòa";
			chuoikq2+="- Hòa";
			MainActivity.mSound.playRandom();
			dialog.showDrawDialog(this);
		}
		
		tv1.setText(chuoikq1);
		tv2.setText(chuoikq2);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}
	
	@Override
	public void onBackPressed(){
		super.onBackPressed();
		MainActivity.mSound.playClick();
		mData = new Database(this);
        mData.openDatabase();
        mPlayer = mData.getPlayerByName(userName);
		mPlayer.setDollar(diem);
		mData.updatePlayer(mPlayer);
		mData.closeDatabase();
		Intent i = new Intent(this, MainActivity.class);
		i.putExtra("userName", mPlayer.getName());
		startActivity(i);
		finish();
		
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		mMusic.release();
	}
	public void onPause(){
		super.onPause();
		mMusic.pause();
	}
	public void onResume(){
		super.onResume();
		if(Setting.isMusic) mMusic.resume();
	}
}
